package com.intct.hbase.dml;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.intct.hbase.HBaseConnection;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;

import java.io.IOException;
import java.util.Map;

/**
 * @author gufg
 * @since 2025-11-04 10:53
 *
 * delete '命名空间:表名', 'rowkey', '列族:列限定符'   # 只删除当前版本
 * deleteall '命名空间:表名', 'rowkey'      #删除所有版本
 */
public class DeleteData {
    public static void main(String[] args) throws IOException {
        // 建立连接
        Connection connection = HBaseConnection.connection;

        // TableName实例
        TableName tableName = TableName.valueOf("intct86", "t2");

        // 获取Admin实例
        Admin admin = connection.getAdmin();

        Table table = null;

        try {
            // 判断表存在时
            if (admin.tableExists(tableName)) {
                // 获取Table实例
                table = connection.getTable(tableName);

                // api没有deleteall功能，如果删除一行记录，不用增加列族、列限定符信息
                Delete delete = new Delete("1002".getBytes());
//                delete.addColumn("f1".getBytes(), "name".getBytes());

                table.delete(delete);
            }
        } catch (IOException e) {
            System.out.println("插入数据失败！");
        } finally {
            // 关闭资源
            if (admin != null) {
                admin.close();
            }
            if (table != null) {
                table.close();
            }
        }
    }
}
